package ThreadSafety;

class Counter {
    int count = 0;
    public synchronized void add(){
        count++;
    }

    public int get(){
        return count;
    }
}



public class AddCountWith2Thread {


    public static void main(String[] args) throws InterruptedException {

        Counter counter = new Counter();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                counter.add();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                counter.add();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(counter.get());
    }
}
